package com.dlc.felear.xiaoensale.activity;

import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.adapter.BlueToothItemAdapter;
import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.customView.LVCircularRing;
import com.dlc.felear.xiaoensale.customView.ShowTispMsgDialog;
import com.dlc.felear.xiaoensale.deviceUtil.Serialhelper;
import com.dlc.felear.xiaoensale.dlc_serial_port.DlcSerialPortClient;
import com.dlc.felear.xiaoensale.factory.CmdList;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.dlc.felear.xiaoensale.helper.Session;
import com.dlc.felear.xiaoensale.server.UsbService;
import com.dlc.felear.xiaoensale.utils.TimeUtils;
import com.itdlc.android.library.base.BaseActivity;
import com.itdlc.android.library.utils.APPDidUtil;
import com.itdlc.android.library.utils.AppInfo;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.HashMap;

import butterknife.BindView;

public class CheckDeviceActivity extends BaseActivity implements Handler.Callback {
    private static final String TAG = "CheckDeviceActivity";
    public static final String WIFI_ZH = "wifi_zh";
    public static final String WIFI_MM = "wifi_mm";
    @BindView(R.id.lv)
    ListView lv;
    @BindView(R.id.dh)
    LVCircularRing mLoadingView;
    @BindView(R.id.et_acccount)
    EditText etC;
    @BindView(R.id.et_password)
    EditText etP;
    @BindView(R.id.image)
    TextView textView;
    @BindView(R.id.verdion)
    TextView tvVersion;
    @BindView(R.id.hwzl)
    TextView tvHwzl;
    @BindView(R.id.lyzl)
    TextView tvLyzl;
    private BlueToothItemAdapter blueToothItemAdapter;
    private int flag = 0;
    private long tempstamp = 0;
    private boolean tempFlag = false;
    private Handler handler = new Handler(this);
    private Session session;
    private ShowTispMsgDialog showTispMsgDialog;

    @Override
    protected int getLayoutRes() {
        session = Session.getSession();
        return R.layout.activity_check_device;
    }

    @Override
    protected void onResume() {
        super.onResume();
        etC.setText(session.getString(WIFI_ZH));
        etP.setText(session.getString(WIFI_MM));
        blueToothItemAdapter = new BlueToothItemAdapter(this);
        lv.setAdapter(blueToothItemAdapter);
        String name=APPDidUtil.getAppDid();
        name=name.substring(name.length()-4);
        tvVersion.setText("设备编号:"+name+"  软件版本号:" + AppInfo.getLocalVersionName(this));
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_ffffff:
                try {
                    tempstamp = System.currentTimeMillis();
                    TextView textView = (TextView) view;
                    if (!tempFlag) {
                        textView.setText("停止搜索蓝牙");
                        EventBus.getDefault().post("start", Constant.SOUSUO_LY);
//                        showShortToast("开始搜索蓝牙");
                    } else {
                        blueToothItemAdapter.clearData();
                        textView.setText("开始搜索蓝牙");
                        setShowms("清除信息");
                        EventBus.getDefault().post("stop", Constant.SOUSUO_LY);
//                        showShortToast("停止搜索蓝牙");
                    }
                    setStatus(!tempFlag);
                    tempFlag = tempFlag ? false : true;
                } catch (Exception e) {
                    Log.e(TAG, e.getMessage());
                }
                break;
            case R.id.s_setting:
                startActivity(new Intent(this, BlueDeviceSettingActivity.class));
                break;
        }

    }

    @Subscriber(tag = Constant.RECIEVE_LY)
    public void recieveData(HashMap<String, String> data) {
        Log.e(TAG, "recieveData:" + JsonHelper.toJson(data) + " 耗时:" + (System.currentTimeMillis() - tempstamp));
        blueToothItemAdapter.setItems(data);
        if(data.isEmpty()){
            setShowms("清除信息");
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        flag = 0;
        return false;
    }


    @Subscriber(tag = Constant.SOUSUO_STATUS)
    private void setStatus(boolean b) {
        try {
            if (b) {
                blueToothItemAdapter.clearData();
                mLoadingView.setVisibility(View.VISIBLE);
                mLoadingView.startAnim();
            } else {
                mLoadingView.setVisibility(View.INVISIBLE);
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }

    StringBuffer stringBuffer = new StringBuffer();

    @Subscriber(tag = Constant.SERIALMSG)
    private void setShowms(String msg) {
        if (TextUtils.equals(msg, "清除信息") && stringBuffer.length() > 0) {
            stringBuffer.delete(0, stringBuffer.length() - 1);
            textView.setText(stringBuffer.toString());
            return;
        }
        stringBuffer.insert(0, msg + "\n");
        textView.setText(stringBuffer.toString());
    }

    @Subscriber(tag = "HWZL")
    private void showHwzl(String msg) {
        if(TextUtils.isEmpty(msg)){
            tvHwzl.setText("接受红外指令:");
            return;
        }
        tvHwzl.setText("接受红外指令:"+msg+ "   "+TimeUtils.getCurrrentTimeHMS());
    }
    @Subscriber(tag = "LYZL")
    private void showLyzl(String msg) {
        if(TextUtils.isEmpty(msg)){
            tvLyzl.setText("接受蓝牙指令:");
            return;
        }
        tvLyzl.setText("接受蓝牙指令:"+msg+ "   "+TimeUtils.getCurrrentTimeHMS());
    }



    @Subscriber(tag = "USB_LY_INFO")
    private void usbLyInfo(String msg) {
        Log.e(TAG,"usbLyInfo:"+msg);
        if(TextUtils.equals(msg,UsbService.ACTION_USB_READY)){
            dialogShow_Closed("USB蓝牙模块无法连接，请检查！！！",1);
        }else {
            dialogShow_Closed("",-1);
        }
    }

    private void dialogShow_Closed(String msg,int flag){
//        if(showTispMsgDialog==null){
//            showTispMsgDialog=new ShowTispMsgDialog(this);
//        }
//        if(flag>0){
//            showTispMsgDialog.setDialog(msg);
//        }else {
//            showTispMsgDialog.closeDialog();
//        }
    }
}